//
//  SettingViewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/4/9.
//

import UIKit

class SettingViewController: CommonViewController {
    private let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)
    private var dataList:Array<Array<SetItemModel>> = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.title = "设置"
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints({
            $0.edges.equalToSuperview()
        })
        self.navigationController?.navigationBar.setBackgroundImage(UIColor.white.toImage(), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = Separator_COLOR.toImage()
        tableView.backgroundColor = BACKGROUND_COLOR
        tableView.separatorColor = Separator_COLOR
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(SetItemArrowCell.self, forCellReuseIdentifier: "SettingArrowCell")
        tableView.register(SetItemSwitchCell.self, forCellReuseIdentifier: "SettingSwitchCell")
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.rowHeight = 44.0
        var firstlist = [SetItemModel.init(type: .Arrow, title: "隐私",className: "PrivacSetViewController")]
        if DataManager.login {
            firstlist.insert(SetItemModel.init(type: .Arrow, title: "账号与安全",className: "SecurityViewController"), at: 0)
            firstlist.insert(SetItemModel.init(type: .Arrow, title: "编辑资料"), at: 0)
        }
        dataList.append(firstlist)
        dataList.append([SetItemModel.init(type: .Switch, title: "夜间模式",isOn: false),SetItemModel.init(type: .Arrow, title: "字体大小",tip: "小")])
        dataList.append([SetItemModel.init(type: .Arrow, title: "清除缓存",tip:"100MB"),SetItemModel.init(type: .Arrow, title: "播放与网络设置",className: "PlayerAndNetSetViewController"),SetItemModel.init(type: .Arrow, title: "推送通知设置",className: "NoticeSetViewController"),SetItemModel.init(type: .Switch, title: "提示音开关")])
        
        dataList.append([SetItemModel.init(type: .Switch, title: "H5广告过滤", subTitle: "智能过滤网站广告,为你节省更多流量"),SetItemModel.init(type: .Arrow, title: "头条封面",className: "CoverViewController")])
        
        dataList.append([SetItemModel.init(type: .Arrow, title: "检查版本",tip: "1.10.1"){
            let hud = MMProgressHUD.showHUDonView()
            hud.backgroundColor = .clear
            hud.model = .Text
            hud.title = "已是最新版本"
            // delay 时间后自动隐藏
            hud.hideHUD(delay: 2)
        },SetItemModel.init(type: .Arrow, title: "关于头条",className: "AboutSetViewController")])
        if DataManager.login {
            dataList.append([SetItemModel.init(type: .Noraml, title: "退出登录"){[weak self] in
                let alert = UIAlertController.init(title: "退出确认", message: "退出当前头条账号，将不能同步收藏，发布评论和云端分享", preferredStyle: .alert)
                let cancel = UIAlertAction.init(title: "取消", style: .cancel)
                let out = UIAlertAction.init(title: "确认退出", style: .default) { (al) in
                    DataManager.login = false
                    NotificationMananger.post(.LoginCompletion)
                    self?.navigationController?.popViewController(animated: true)
                }
                alert.addAction(cancel)
                alert.addAction(out)
                self?.present(alert, animated: true, completion: {
                    
                })
            }])
        }
        let footLabel = UILabel()
        footLabel.text = "www.honchuang.cn"
        footLabel.textAlignment = .center
        footLabel.font = Font_Size_12
        footLabel.textColor = B999_TITLE_COLOR
        footLabel.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: 60)
        tableView.tableFooterView = footLabel
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    override func viewDidDisappear(_ animated: Bool) {
        
    }
    
}
extension SettingViewController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10.0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

        return 0.001
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let item = dataList[indexPath.section][indexPath.row]
        if(item.subTitle == ""){
            return 44.0
        }else{
            return 60.0
        }
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = dataList[indexPath.section][indexPath.row]
        if item.className != "" {
            let vc = String.ClassFromString(item.className)
            self.pushViewController(vc! , animated: true)
        }else{
            item.closure?()
        }
        tableView.deselectRow(at: indexPath, animated: false)
    }
}
extension SettingViewController:UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList[section].count
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataList.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = dataList[indexPath.section][indexPath.row]
        if item.type == .Arrow {
            let cell = tableView.dequeueReusableCell(withIdentifier: "SettingArrowCell", for: indexPath) as? SetItemArrowCell
            cell?.binding(item)
            return cell!
        }else if item.type == .Switch{
            let cell = tableView.dequeueReusableCell(withIdentifier: "SettingSwitchCell", for: indexPath) as? SetItemSwitchCell
            cell?.binding(item,indexPath:indexPath)
            return cell!
        }
        else{
            var cell = tableView.dequeueReusableCell(withIdentifier: "cess")
            if cell==nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "cess")
                let label = UILabel()
                label.text = "退出登录"
                label.textAlignment = .center
                label.textColor = RED_COLOR
                label.font = Font_Size_14
                cell?.contentView.addSubview(label)
                label.snp.makeConstraints({
                    $0.edges.equalToSuperview()
                })
            }
            return cell!
        }
    }
}

